//
//  KLToBeDoctorViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLToBeDoctorViewController.h"
#import "STTextView.h"
#define XInsetValeu 20
#define nomalHeight 44
@interface KLToBeDoctorViewController ()
@property(nonatomic, strong) UITextField                     *nameTextFiled;
@property(nonatomic, strong) UITextField                     *phoneTextFiled;
@property(nonatomic, strong) UITextField                     *typeTextFiled;

@property(nonatomic, strong) STTextView                     *goodTextView;
@property(nonatomic, strong) STTextView                     *destextView;

@property(nonatomic, strong) UIScrollView                       *imageScrollView;
@property(nonatomic, strong) NSMutableArray                     *imageArray;
@property(nonatomic, assign)    CGFloat                         imageViewRight;
@property(nonatomic, strong) NSMutableArray                     *imageUrlsArray;
@end

@implementation KLToBeDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善资料";
    self.imageArray = [NSMutableArray new];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView st_autoAdjustAllResponder];
    [self configSubView];
    [self configFooter];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    header.backgroundColor = [UIColor whiteColor];
    
    
    UIView * namesView = [self headerViewWithTitle:@"姓名:"];
    namesView.top = 0;
    [header addSubview:namesView];
    self.nameTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, namesView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.nameTextFiled.placeholder = @"请输入姓名";
    [header addSubview:self.nameTextFiled];
    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.nameTextFiled.bottom, UIScreenWidth, 0.5)];
    line1.backgroundColor = KL_lineColor;
    [header addSubview:line1];
    
    UIView * phoneView = [self headerViewWithTitle:@"联系电话:"];
    phoneView.top = line1.bottom;
    [header addSubview:phoneView];
    self.phoneTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, phoneView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.phoneTextFiled.placeholder = @"请输入电话号码";
    self.phoneTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.phoneTextFiled];
    UIView * line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.phoneTextFiled.bottom, UIScreenWidth, 0.5)];
    line2.backgroundColor = KL_lineColor;
    [header addSubview:line2];
    
    UIView * typeView = [self headerViewWithTitle:@"科室:"];
    typeView.top = line2.bottom;
    [header addSubview:typeView];
    self.typeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(XInsetValeu, typeView.bottom, UIScreenWidth - 2*XInsetValeu, 30)];
    self.typeTextFiled.placeholder = @"请输入科室";
    [header addSubview:self.typeTextFiled];
    UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, self.typeTextFiled.bottom, UIScreenWidth, 0.5)];
    line3.backgroundColor = KL_lineColor;
    [header addSubview:line3];
    
    UIView * goofView = [self headerViewWithTitle:@"擅长:"];
    goofView.top = line3.bottom;
    [header addSubview:goofView];
    self.goodTextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, goofView.bottom, UIScreenWidth - 2*XInsetValeu, 60)];
    self.goodTextView.placeholder = @"请输入擅长区域（至少20字）";
    [self.goodTextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.goodTextView];
    
    UIView * desView = [self headerViewWithTitle:@"简介"];
    desView.top = self.goodTextView.bottom;
    [header addSubview:desView];
    self.destextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, desView.bottom, UIScreenWidth - 2*XInsetValeu, 60)];
    self.destextView.placeholder = @"请输入您的简介";
    [self.destextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.destextView];
    
    header.height = self.destextView.bottom;
    self.tableView.tableHeaderView = header;
}
- (void)configFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STLabel * imagLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, UIScreenWidth - 2* self.insetX, 44)
                                                    text:@"上传资质图片:(提示，至少上传三张图片)"
                                               textColor:KL_firstTextColor
                                                    font:14
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [footer addSubview:imagLable];
    
    //封面
    self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.insetX, imagLable.bottom, UIScreenWidth- 2*self.insetX, 110)];
    self.imageScrollView.contentSize = CGSizeMake(self.imageScrollView.width, 0);
    [footer addSubview:self.imageScrollView];
    [self resetImageScrollViewFrameWithAddImages:nil withScrollView:self.imageScrollView];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, self.imageScrollView.bottom + 30, UIScreenWidth - 60 , 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom + 40;
    self.tableView.tableFooterView = footer;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    backView.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, 44)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
- (void)onSelectedCommitButton{
    if (self.nameTextFiled.text.length < 2) {
        [SVProgressHUD showErrorWithStatus:@"姓名不能少于两个字"];
        return;
    }
    if (self.phoneTextFiled.text.length < 6 || self.phoneTextFiled.text.length > 20) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (self.typeTextFiled.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入科室"];
        return;
    }
    if (self.goodTextView.text.length < 20) {
        [SVProgressHUD showErrorWithStatus:@"擅长领域至少20字"];
        return;
    }
    if (self.destextView.text.length < 20) {
        [SVProgressHUD showErrorWithStatus:@"简介至少20字"];
        return;
    }
    if (self.imageArray.count < 3) {
        [SVProgressHUD showErrorWithStatus:@"至少三张专家证明图片"];
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    if ([self isCanSendRequest]) {
        [self sendTobeDoctorRequest];
        return;
    }
    [self sendImages];

}
#pragma mark --imageView
- (void)resetImageScrollViewFrameWithAddImages:(NSArray*)images withScrollView:(UIScrollView*)scrollView{
    
    for (UIView * view in scrollView.subviews) {
        [view removeFromSuperview];
    }
    //添加addImageView
    __weak typeof(self) weakSelf =  self;
    STButton * addImageButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
                                                          title:@""
                                                     titleColor:nil
                                                      titleFont:0
                                                   cornerRadius:0
                                                backgroundColor:KL_backgroundColor
                                                backgroundImage:nil
                                                          image:[UIImage imageNamed:@"sz_xiangce"]];
    addImageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [addImageButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddImageButton:sender];
    }];
    [scrollView addSubview:addImageButton];
    if (scrollView == self.imageScrollView) {
        self.imageViewRight = 100;
    }
    for (UIImage * image in images) {
        CGFloat right = 0;
        right = self.imageViewRight + 10;
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(right, 0, 100, 100)];
        imageView.image = image;
        [scrollView addSubview:imageView];
        self.imageViewRight = imageView.right;
        scrollView.contentSize = CGSizeMake(self.imageArray.count * 110 + 110, 0);
        
    }
    
}
- (void)onSelectedAddImageButton:(UIButton*)button{
    [self showSTImagePickerHandle:^(NSArray<STPhotoModel *> *array) {
        NSMutableArray *newImages = [NSMutableArray new];
        if (button.superview == self.imageScrollView) {
            self.imageArray = [NSMutableArray new];
        }
        for (STPhotoModel * model in array) {
            UIImage * image = model.originImage;
            image = image?image:model.thumbImage;
            if (button.superview == self.imageScrollView) {
                
                [self.imageArray addObject:image];
            }
            
            [newImages addObject:image];
        }
        
        [self resetImageScrollViewFrameWithAddImages:newImages withScrollView:(UIScrollView*)button.superview];
        
    } pickerBlock:^(UIImage *image) {
       // self.imageArray = [NSMutableArray new];
        if (button.superview == self.imageScrollView) {
            [self.imageArray addObject:image];
        }
        [self resetImageScrollViewFrameWithAddImages:self.imageArray withScrollView:(UIScrollView*)button.superview];
    }];
}
- (void)sendImages{
    self.imageUrlsArray = [NSMutableArray new];
    for (UIImage * image in self.imageArray) {
        [KLUtils sendQiNiuWithImage:image handle:^(NSString *imageUrl, NSString *errorDes) {
            [self.imageUrlsArray addObject:imageUrl];
            if ([self isCanSendRequest]) {
                [self sendTobeDoctorRequest];
            }
        }];
    }
}
- (BOOL)isCanSendRequest{
    if (self.imageUrlsArray.count == self.imageArray.count ) {
        return YES;
    }
    return NO;
}
#pragma mark --NetWork Method
- (void)sendTobeDoctorRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (self.nameTextFiled.text.length ) {
        [dic setObject:self.nameTextFiled.text forKey:@"name"];
    }
    if (self.phoneTextFiled.text.length) {
        [dic setObject:self.phoneTextFiled.text forKey:@"phone"];
    }
    if (self.typeTextFiled.text.length ) {
        [dic setObject:self.typeTextFiled.text forKey:@"department"];
    }
    if (self.goodTextView.text.length ) {
        [dic setObject:self.goodTextView.text forKey:@"adept"];
    }
    if (self.destextView.text.length ) {
        [dic setObject:self.destextView.text forKey:@"introduce"];
    }
    
    NSString * cover_map = [self.imageUrlsArray componentsJoinedByString:@","];
    [dic setObject:cover_map forKey:@"pictures"];
    
    
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/doctor/applydoctor",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"申请成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                             }];
}
@end

